package Stack.Medium;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Stack;

public class LC0739 {
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new LinkedList<>(); // 利用非严格递减单调栈
        int[] ret = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            int temp = temperatures[i];
            while (!stack.isEmpty() && temperatures[stack.peek()] < temp) {
                int poppedIdx = stack.pop();
                ret[poppedIdx] = i - poppedIdx;
            }
            stack.push(i);
        }

        return ret;
    }

    public static void main(String[] args) {
        int[] temperatures = new int[]{73, 74, 75, 71, 69, 72, 76, 73};
        LC0739 solver = new LC0739();
        solver.dailyTemperatures(temperatures);
    }
}
